clear
use "${data}IndirectSurveyDataReplication.dta", clear


keep if complete == 1



******************* FIGURES *******************

twoway (lpolyci IPW_leaderabbr IPW_HHabbr, fcolor(none) clcolor(ekblue)) (line IPW_HH IPW_HH), graphregion(color(white)) ytitle("Leader IPW index") xtitle("Household IPW index") ///
legend(order(1 2) cols(1)  label(1 "95% CIs") label(2 "Kernel regression"))  yscale(range(0 .50 2))
graph 	save "${outputs}povIPWabbr.gph", replace


twoway (lpolyci PIB_leaderabbr PIB_HHabbr, fcolor(none) clcolor(ekblue)) (line PIB_HH PIB_HH), graphregion(color(white)) ytitle("Leader binary sum index") xtitle("Household binary sum index") ///
legend(order(1 2) cols(1) label(1 "95% CIs") label(2 "Kernel regression")) 
graph 	save "${outputs}povPIBabbr.gph", replace

twoway (lpolyci PCA_leaderabbr PCA_HHabbr, fcolor(none) clcolor(ekblue)) (line PCA_HH PCA_HH), graphregion(color(white)) ytitle("Leader PCA index") xtitle("Household PCA index") ///
legend(order(1 2) cols(1) label(1 "95% CIs") label(2 "Kernel regression")) 
graph 	save "${outputs}povPCAabbr.gph", replace



gr combine 	"${outputs}povPIBabbr.gph" "${outputs}povIPWabbr.gph" "${outputs}povPCAabbr.gph", ///
			graphregion(color(white))
gr export "${outputs}poverty_indicesabbr.eps", replace

estimates clear


***************** LOOK AT CLASSIFICATION ERROR IN INDICES ****************************

estpost tab poor_PIB_leaderabbr poor_PIB_HHabbr
esttab . using "${outputs}error_povabbr.tex", replace cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment collabels("" "" "" )  noobs ///
  prehead( & \multicolumn{3}{c}{Household responses} \\ ) ///
  posthead(Leader responses & \\ \hline \multicolumn{4}{l}{Simple index} \\ ) ///
  postfoot(\hline) 

  estpost tab poor_IPW_leaderabbr poor_IPW_HHabbr
esttab .  using "${outputs}error_povabbr.tex", append cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment  collabels( "" "") noobs ///
 posthead(\hline \multicolumn{4}{l}{Inverse proportion weighted index} \\ ) ///
postfoot(\hline)  
  
estpost tab poor_PCA_leaderabbr poor_PCA_HHabbr
esttab .  using "${outputs}error_povabbr.tex", append cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment  collabels( "" "") noobs ///
 posthead(\hline \multicolumn{4}{l}{Principle components index} \\ ) ///
 postfoot( \hline \hline \end{tabular} \begin{tablenotes}[para,flushleft] \footnotesize{Cells show percentages in each category. ///
 In all cases, the poor are those with less than the median level of the index ///
	calculated across the entire sample using the index  developed from household reports.} \end{tablenotes} )



foreach x in PIB IPW PCA {
gen error_`x'_exclu = poor_`x'_HHabbr == 1 & poor_`x'_leaderabbr == 0
replace error_`x'_exclu = . if poor_`x'_HHabbr == 0 | poor_`x'_leaderabbr == .
gen error_`x'_inclu = poor_`x'_HHabbr == 0 & poor_`x'_leaderabbr == 1
replace error_`x'_inclu = . if poor_`x'_HHabbr == 1 | poor_`x'_leaderabbr == .
}

la var num_ejidatarios_IHS "IHS(Community members w/ full rights)"


foreach x in error_PIB_exclu error_IPW_exclu error_PCA_exclu error_PIB_inclu error_IPW_inclu error_PCA_inclu {
reg `x' num_ejidatarios_IHS, cluster(ejnclnmid)
eststo networks`x'
reg `x' meancarg, cluster(ejnclnmid)
eststo elite`x'
reg `x' soc_cap_simple, cluster(ejnclnmid)
eststo sc`x'
lincom soc_cap_simple*0.12
reg `x' lider_educ lider_edad , cluster(ejnclnmid)
eststo educ`x'
lincom lider_educ*.47
lincom lider_edad*10.7
reg `x' num_ejidatarios_IHS meancarg soc_cap_simple lider_educ lider_edad , cluster(ejnclnmid)
eststo all`x'

sum `x' 
estadd scalar m = r(mean)

}


esttab networks*  ///
	using "${outputs}index_heterogeneityabbr.tex",  ///
	se nonotes  style(tex)  b(%12.3f) se(%12.3f)  noobs ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "" ) ///
	 keep(num_ejidatarios_IHS) nonumbers  replace  fragment  ///
	prehead( { \begin{tabular}{lcccccc} \hline \hline ///
	 & \multicolumn{3}{l}{Exclusion error} & \multicolumn{3}{l}{Inclusion error} \\ ///
	 & Binary & IPW & PCA & Binary & IPW & PCA \\ \hline ) ///
	 posthead(A: Community size & \\ [1em]) ///

esttab elite*  ///
	using "${outputs}index_heterogeneityabbr.tex",  ///
	se nonotes  style(tex)  b(%12.3f) se(%12.3f)  noobs ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "" ) ///
	 keep(meancarg)  nonumbers  append  fragment ///
	 posthead(\hline B: Leadership capture & \\  [1em]) 

esttab sc*  ///
	using "${outputs}index_heterogeneityabbr.tex",  ///
	se nonotes  style(tex)  b(%12.3f) se(%12.3f)  noobs ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "") ///
	 keep(soc_cap_simple)  nonumbers  append  fragment ///
	 posthead(\hline C: Social capital &  \\ [1em] ) 

	 
esttab educ*  ///
	using "${outputs}index_heterogeneityabbr.tex",  ///
	se nonotes  style(tex)  b(%12.3f) se(%12.3f)  noobs ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "") ///
	 keep(lider_educ lider_edad)  nonumbers  append  fragment ///
	 posthead(\hline D: Education/age &  \\ [1em] ) ///
stats(N m, fmt(%12.0f %12.3f) labels("Observations" "Mn dep. var.")) ///
	 prefoot( \hline \hline ) ///
	postfoot(\hline \hline \end{tabular} } \begin{tablenotes}[para,flushleft] \footnotesize{Regressors are binary variables indicating  ///
	errors of inclusion or exclusion. Standard errors are clustered at the community level and regressions are simple OLS ///
	with a constant. Regressions are run on the sub-sample of households identified as poor (exclusion error) or non-poor ///
	(inclusion error) according to the household data. All panels contain only the covariates reported and a constant. * p ///
	$<$.10, ** p$<$ .05, *** p$<$.01. } \end{tablenotes} )

esttab all* ///
	using "${outputs}index_heterogeneity_mainabbr.tex", replace ///
	keep( num_ejidatarios_IHS meancarg soc_cap_simple lider_edad lider_educ) ///
	se nonotes  style(tex)  b(%12.3f) se(%12.3f)  fragment  nonumbers ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "") ///
	stats(N m, fmt(%12.0f %12.3f) labels("Observations" "Mn dep. var.")) ///
	prehead( { \begin{tabular}{lcccccc} \hline \hline ///
	 & \multicolumn{3}{l}{Exclusion error} & \multicolumn{3}{l}{Inclusion error} \\ ///
	 & Binary & IPW & PCA & Binary & IPW & PCA \\ ) ///
	postfoot(\hline \hline \end{tabular} } \begin{tablenotes}[para,flushleft] \footnotesize{Regressors are binary variables indicating  ///
	errors of inclusion or exclusion. Standard errors are clustered at the community level and regressions are simple OLS ///
	with a constant. Regressions are run on the sub-sample of households identified as poor (exclusion error) or non-poor ///
	(inclusion error) according to the household data. All panels contain only the covariates reported and a constant. 	* p ///
	$<$.10, ** p$<$ .05, *** p$<$.01. } \end{tablenotes} )
	